const app = getApp()
let qCloud = require("../../../index.js");
Page({

    /**
     * 页面的初始数据
     */
    data: {
        imgPath: qCloud.config.STATIC_IMAGE_PATH,

        postList: [],
        postParams: {
            pageNum: 1,
            pageSize: 20
        },
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.getPostList(this.data.postParams);
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    radioChange(e) {
        let items = this.data.postList
        let value = e.detail.value
        for (let i = 0, lenI = items.length; i < lenI; ++i) {
            if (items[i].postId == value) {
                wx.setStorageSync("weapp_choose_post", {
                    postId: items[i].postId,
                    postName: items[i].postName
                });
                break;
            }
        }
    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {
        let self = this;
        self.setData({
            postParams: {
                pageNum: 1,
                pageSize: 20,
            },
            postList: []
        });
        this.getPostList(self.data.postParams);
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
        let self = this;
        if (self.data.postParams.pageSize == self.data.postList.length) {
            self.setData({
                postParams: {
                    pageNum: self.data.postParams.pageNum + 1,
                    pageSize: 20,
                }
            });
            self.getPostList(self.data.postParams);
        } else {
            return;
        }
    },

    getPostList: function (params) {
        let self = this;
        app.fetchApis(self, qCloud.api.getPostList, params, "GET", function (e) {
            if (e.data.code == 200) {
                let postList = e.data.rows;
                postList = [...self.data.postList, ...postList]
                self.setData({
                    postList: postList
                })
            }
        })
    },

    doSubmit() {
        wx.navigateBack({
            delta: 1
        })
    },
})
